<?php
namespace App\Helpers\Api;

use Symfony\Component\HttpFoundation\Response as FoundationResponse;
use Response;

trait ApiResponse
{
    /**
     * @var int
     */
    protected $statusCode = FoundationResponse::HTTP_OK;

    /**
     * @param $code
     * @param null $data
     * @param string $message
     * @return mixed
     */
    public function json($code, $data=null, $message='success'){
        if($code != $this->statusCode || $message != 'success'){
            $message = config('statuscode.'.$code)?:'';
        }

        $return = [
            'code'=>$code,
            'message'=>$message,
        ];
        if($data){
            $return['data'] = $data;
        }
        return Response::json($return,$this->statusCode);
    }
}